package com.ruoyi.dmsIms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dmsIms.domain.vo.SimpleProList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dmsIms.domain.ProInfo;
import com.ruoyi.dmsIms.service.IProInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 * 
 * @author ZhangSiRui
 * @date 2021-12-08
 */
@RestController
@RequestMapping("/dmsIms/info")
public class ProInfoController extends BaseController
{
    @Autowired
    private IProInfoService proInfoService;
    private static final Logger logger = LogManager.getLogger(ProInfoController.class);
    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProInfo proInfo)
    {
        startPage();
        List<ProInfo> list = proInfoService.selectProInfoList(proInfo);
        return getDataTable(list);
    }
    /**
     * 查询项目信息简易列表
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:list')")
    @GetMapping("/splist")
    public TableDataInfo simplelist(SimpleProList simpleProList)
    {
        List<SimpleProList> list = proInfoService.selectProInfoListSimple(simpleProList);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProInfo proInfo)
    {
        List<ProInfo> list = proInfoService.selectProInfoList(proInfo);
        ExcelUtil<ProInfo> util = new ExcelUtil<ProInfo>(ProInfo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(proInfoService.selectProInfoById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProInfo proInfo)
    {
        return toAjax(proInfoService.insertProInfo(proInfo));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProInfo proInfo)
    {
        return toAjax(proInfoService.updateProInfo(proInfo));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('dmsIms:info:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(proInfoService.deleteProInfoByIds(ids));
    }

    @GetMapping("/canDel/{ids}")
    public AjaxResult canDel(@PathVariable Long[] ids)
    {
        //统计该项目是否有测试计划
        int planCount = proInfoService.countTestPlan(ids);
        //统计该项目是否有测试接口
        int interCount = proInfoService.countInterface(ids);

        Map<String, Integer> result = new HashMap<>();

        result.put("planCount",planCount);
        result.put("interCount",interCount);

        return AjaxResult.success(result);
    }

}
